import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../viewmodels/music_player_view_model.dart';
import '../../models/play_mode.dart';
import 'play_or_pause_widget.dart';

/// 播放控制按钮组件
/// 包含上一首、播放/暂停、下一首等控制按钮
class PlaybackControlsWidget extends StatelessWidget {
  /// 按钮大小
  final double buttonSize;

  /// 主播放按钮大小
  final double mainButtonSize;

  /// 按钮颜色
  final Color? iconColor;

  /// 主播放按钮背景颜色
  final Color? mainButtonColor;

  /// 按钮间距
  final double spacing;

  /// 是否显示播放模式按钮
  final bool showPlayModeButton;

  /// 是否显示播放列表按钮
  final bool showPlaylistButton;

  /// 播放列表按钮点击回调
  final VoidCallback? onPlaylistTap;

  const PlaybackControlsWidget({
    Key? key,
    this.buttonSize = 32,
    this.mainButtonSize = 56,
    this.iconColor,
    this.mainButtonColor,
    this.spacing = 24,
    this.showPlayModeButton = true,
    this.showPlaylistButton = true,
    this.onPlaylistTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer<MusicPlayerViewModel>(
      builder: (context, viewModel, child) {
        return Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 播放模式按钮
            if (showPlayModeButton) ...[
              _buildPlayModeButton(context, viewModel),
              SizedBox(width: spacing),
            ],

            // 上一首按钮
            _buildControlButton(
              icon: Icons.skip_previous,
              onPressed: viewModel.canPlayPrevious ? viewModel.previous : null,
              size: buttonSize,
            ),

            SizedBox(width: spacing),

            // 播放/暂停按钮
            PlayOrPauseWidget(
              size: mainButtonSize,
              iconColor: Colors.white,
              backgroundColor: mainButtonColor,
            ),

            SizedBox(width: spacing),

            // 下一首按钮
            _buildControlButton(
              icon: Icons.skip_next,
              onPressed: viewModel.canPlayNext ? viewModel.next : null,
              size: buttonSize,
            ),

            // 播放列表按钮
            if (showPlaylistButton) ...[
              SizedBox(width: spacing),
              _buildControlButton(
                icon: Icons.queue_music,
                onPressed: onPlaylistTap,
                size: buttonSize,
              ),
            ],
          ],
        );
      },
    );
  }

  /// 构建控制按钮
  Widget _buildControlButton({
    required IconData icon,
    required VoidCallback? onPressed,
    required double size,
  }) {
    return IconButton(
      icon: Icon(
        icon,
        color: iconColor,
        size: size,
      ),
      onPressed: onPressed,
      iconSize: size,
    );
  }

  /// 构建播放模式按钮
  Widget _buildPlayModeButton(
      BuildContext context, MusicPlayerViewModel viewModel) {
    return ValueListenableBuilder(
      valueListenable: viewModel.playMode,
      builder: (context, playMode, child) {
        return IconButton(
          icon: Icon(
            playMode.icon,
            color: iconColor,
            size: buttonSize,
          ),
          onPressed: viewModel.togglePlayMode,
          tooltip: playMode.displayName,
        );
      },
    );
  }
}

/// 紧凑版播放控制组件
/// 适用于悬浮播放器等空间有限的场景
class CompactPlaybackControlsWidget extends StatelessWidget {
  /// 按钮大小
  final double buttonSize;

  /// 按钮颜色
  final Color? iconColor;

  /// 按钮间距
  final double spacing;

  const CompactPlaybackControlsWidget({
    Key? key,
    this.buttonSize = 24,
    this.iconColor,
    this.spacing = 8,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer<MusicPlayerViewModel>(
      builder: (context, viewModel, child) {
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 上一首按钮
            IconButton(
              icon: Icon(
                Icons.skip_previous,
                color: iconColor,
                size: buttonSize,
              ),
              onPressed: viewModel.canPlayPrevious ? viewModel.previous : null,
              constraints: BoxConstraints(
                minWidth: buttonSize + 8,
                minHeight: buttonSize + 8,
              ),
              padding: EdgeInsets.zero,
            ),

            SizedBox(width: spacing),

            // 播放/暂停按钮
            PlayOrPauseWidget(
              size: buttonSize + 8,
              iconColor: iconColor,
              showBackground: false,
            ),

            SizedBox(width: spacing),

            // 下一首按钮
            IconButton(
              icon: Icon(
                Icons.skip_next,
                color: iconColor,
                size: buttonSize,
              ),
              onPressed: viewModel.canPlayNext ? viewModel.next : null,
              constraints: BoxConstraints(
                minWidth: buttonSize + 8,
                minHeight: buttonSize + 8,
              ),
              padding: EdgeInsets.zero,
            ),
          ],
        );
      },
    );
  }
}
